#include <arpa/inet.h>
#include <stdio.h>
#include <sys/socket.h>
#include <string.h>
#include <errno.h>
#include <string.h>
#include <stddef.h>

#include "log.h"

const char *sockaddrNtop(const void *addr, char *buf, size_t buflen)
{
	const char *ret;

	switch (((struct sockaddr *)addr)->sa_family) {
	case AF_INET:
		ret = inet_ntop(AF_INET, &((struct sockaddr_in *)addr)->sin_addr, buf, buflen);
		break;
	case AF_INET6:
		ret = inet_ntop(AF_INET6, &((struct sockaddr_in6 *)addr)->sin6_addr, buf, buflen);
		break;
	default:
		snprintf(buf, buflen, "unknown address");
		return buf;
	}

	if (!ret) {
		LOG(LOG_DEBUG, "inet_ntop error: %s", strerror(errno));
		return NULL;
	}

	return buf;
}
